'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)

	/* const appid = 'wx446f460c4717b715';
	const secret = 'da09aa7c9c8d541e8c92a72f19f26aab';
	const apiUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret +
		'&js_code=' + event.code + '&grant_type=authorization_code';
	const res = await uniCloud.httpclient.request(apiUrl, {
		method: 'GET',
		data: {},
		contentType: 'json', // 指定以application/json发送data内的数据
		dataType: 'json' // 指定返回值为json格式，自动进行parse
	}).then(res => {
		return res
	}) */


	let js_code = event.code
	const appid = 'wx446f460c4717b715' //appid  
	const secret = 'da09aa7c9c8d541e8c92a72f19f26aab' //secret  
	const loginUrl = 'https://api.weixin.qq.com/sns/jscode2session'

	let res = await uniCloud.httpclient.request(loginUrl, {
		data: {
			appid: appid,
			secret: secret,
			js_code: js_code,
			grant_type: 'authorization_code'
		},
		dataType: 'json'
	})

	//用户openid
	console.log('openid', res.data.openid)
	//返回数据给客户端
	return {
		openid: res.data.openid
	}

};
